;;; init.el --- Load the full configuration -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(let ((minver "25.1"))
  (when (version< emacs-version minver)
    (error "Your Emacs is too old -- this config requires v%s or higher" minver)))
(when (version< emacs-version "26.1")
`:  (message "Your Emacs is old, and some functionality in this config will be disabled. Please upgrade if possible."))

;;; emacs config
(electric-pair-mode t)  ;; auto complete paren
(add-hook 'prog-mode-hook #'show-paren-mode)  ;; hight the other paren
(column-number-mode t)  ;; show column number in the 'Mode line'
(global-auto-revert-mode t)  ;; refresh buffer when program was changed by other application
(delete-selection-mode t)  ;; replace selected text when typing
(icomplete-mode t)  ;; use complete-completion
(add-hook 'prog-mode-hook #'hs-minor-mode)  ;; when program-mode can fold codes

;;; set window size
(defun set-frame-size-according-to-resolution ()
  "Set Emacs window size acording to resolution."
  (interactive)
  (if window-system
	(if (> (x-display-pixel-width) 1280)
	    (progn (add-to-list 'default-frame-alist
				(cons 'width (/ (- (x-display-pixel-width) 120) (frame-char-width))))
		   (add-to-list 'default-frame-alist
				(cons 'height (/ (- (x-display-pixel-height) 120) (frame-char-height))))))))
(set-frame-size-according-to-resolution)

;;; proxy config function
(defun set-mac-os-proxy ()
  "Config http & https proxy serverice."
  (setq url-proxy-services
	'(("http" . "127.0.0.1:8889")
	  ("https" . "127.0.0.1:8889"))))

(defun set-company-os-proxy()
  "Config http & https proxy serverice."
  (setq url-proxy-services
	'(("http" . "http://proxy-dmz.intel.com:911")
	  ("https" . "http://proxy-dmz.intel.com:911"))))

;;; config for my-mac
(defun config-mac-os()
  "Detect os and make difference config."
  (interactive)
  (if (eq system-type 'darwin)
      (progn (setq mac-command-modifier 'meta)
	     (set-mac-os-proxy))))

(defun config-os ()
  "Config os configuration."
  (if (eq system-type 'darwin)
      (config-mac-os)))
(config-os)

;;; disalbe auto backup
(setq make-backup-files nil)
;;; display line number
(global-linum-mode t)

;; load themes
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'monokai t)

(add-to-list 'load-path (expand-file-name "lisp" user-emacs-directory))

(require 'package)
;;(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;;(package-initialize)

(use-package which-key
  :ensure t
  :init (which-key-mode))

(use-package company
  :ensure t
  :init (global-company-mode))

(use-package smooth-scrolling
  :ensure t
  :config (setq smooth-scrolling-mode 1))

(use-package undo-tree
  :ensure t
  :init (global-undo-tree-mode)
  :config
  (setq undo-tree-auto-save-history nil))

(use-package ace-window
  :ensure t
  :bind (("C-x o" . 'ace-window)))

(use-package flycheck
  :ensure t
  :init (global-flycheck-mode))

(use-package ivy
  :ensure t
  :init
  (ivy-mode))

(use-package projectile
  :ensure t
  :init
  (projectile-mode +1)
  :bind (:map projectile-mode-map
	      ("C-c p" . projectile-command-map)))

(use-package treemacs
  :ensure
  :defer t
  :config
  (treemacs-select-window)
  :bind
  (:map global-map
	("M-0"     . treemacs-select-window)
	("C-x t 1" . treemacs-no-delete-other-windows)
	("C-x t t" . treemacs)
	("C-x t B" . treemacs-bookmark)
	;;("C-x t F" . treemacs-find-file)
	("C-x M-t" . treemacs-find-tag)))

(use-package lsp-mode
  :ensure t
  :init
  ;; set prefix for lsp-keymod-prefix (few alternatives "C-l", "C-c l")
  (setq lsp-keymap-prefix "C-c l"))

(use-package c++-mode
  :ensure t)

(use-package eglot)
(add-hook 'c-mode-hook 'eglot-ensure)
(add-hook 'c++-mode-hook 'eglot-ensure)

(use-package lsp-treemacs
  :ensure t
  :init (lsp-treemacs-sync-mode))

(provide 'init)
